﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YogaDictionary.Common;

namespace YogaDictionary.Model
{
    static class GlossaryInitializer
    {
        private static readonly Dictionary<string, Action<string>> GlossariesList = new Dictionary<string, Action<string>>
        {
            {"Test", InitializeTest },
            {"Словарь Йоги. Эрнест Вуд.", InitializeSlovarYogiErnestWood },
        };

        public static void InitializeAll(bool force = false)
        {
            foreach (var glossary in GlossariesList)
            {
                Initialize(glossary.Key, force);
            }
        }

        public static void Initialize(string glossary, bool force = false)
        {
            var glossaryId = StartInitialization(glossary, force);
            if (glossaryId == 0)
                return;

            GlossariesList[glossary].Invoke(glossary);
        }

        private static long StartInitialization(string glossaryName, bool force)
        {
            var glossary = DataRepo.GetGlossary(glossaryName);
            if (glossary != null)
                if (force)
                    DataRepo.RemoveGlossary(glossary.GlossaryId);
                else
                    return 0;

            Log.Debug("Recreate table: " + glossaryName);
            return 1;
        }

        private static void InitializeTest(string glossaryName)
        {
            var glossaryId = DataRepo.AddGlossary(glossaryName, "Test author", "Test description");
            var definitionId = DataRepo.AddDefinition(glossaryId);
            DataRepo.AddWord(definitionId, "word1");
            DataRepo.AddWord(definitionId, "word2");
            DataRepo.AddWord(definitionId, "word3", false);
            DataRepo.AddDescription(definitionId, @"description1");
            DataRepo.AddDescription(definitionId, @"description2");

            definitionId = DataRepo.AddDefinition(glossaryId);
            DataRepo.AddWord(definitionId, "word4");
            DataRepo.AddWord(definitionId, "word5");
            DataRepo.AddDescription(definitionId, @"description3", false);
        }

        private static void InitializeSlovarYogiErnestWood(string glossaryName)
        {

        }

    }
}
